package com.mnxc.gxa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mnxc.gxa.dto.QueryRoleDTO;
import com.mnxc.gxa.dto.Result;
import com.mnxc.gxa.entity.Role;
import com.mnxc.gxa.entity.User;
import com.mnxc.gxa.mapper.RoleMapper;
import com.mnxc.gxa.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mnxc
 * @since 2023-06-29
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
@Resource
RoleMapper roleMapper;

    @Override
    public Result saveRole(Role role) {
        boolean isSuccess = save(role);
        if (!isSuccess) {
            return Result.fail("添加用户失败！");
        }else {
            return Result.ok();
        }
    }

    @Override
    public Result deleteRole(Role role) {
        boolean isSuccess = roleMapper.deleteRole(role);
        if (!isSuccess) {
            Result.fail("删除角色失败！");
        }
        return Result.ok("删除角色成功！");
    }
    @Override
    public Result updateRole(Role role) {
        boolean isSuccess = updateById(role);
        if (!isSuccess) {
            return Result.fail("更新角色信息失败！");
        }
        return Result.ok("更新角色信息成功！");
    }


    @Override
    public Result freezeRole(Role role) {
        boolean isSuccess = roleMapper.freezeRole(role);
        if (!isSuccess) {
            Result.fail("冻结用户失败！");
        }
        return Result.ok("冻结用户成功！");
    }

    @Override
    public Result getByPageAndCondition(int page, int pageSize, QueryRoleDTO query) {
        //1.构造分页器
        IPage<Role> pageInfo = new Page<>(page,pageSize);
        //2.条件查询
        LambdaQueryWrapper<Role> lqw = new LambdaQueryWrapper<>();
        lqw.like(query.getName()!=null,Role::getName,query.getName())
                .like(query.getTitle()!=null,Role::getTitle,query.getTitle())
                .eq(query.getStatus()!=null,Role::getStatus,query.getStatus());
        //查询基本信息
        this.page(pageInfo,lqw);
        return Result.ok(pageInfo);
    }

    @Override
    public Result activeRole(Role role) {
        boolean isSuccess = roleMapper.activeRole(role);
        if (!isSuccess) {
            Result.fail("激活角色失败！");
        }
        return Result.ok("激活角色成功！");
    }

    @Override
    public Result getAll() {
        // 1.获取数据
        List<Role> roles = roleMapper.getAll();
        // 2.返回
        return Result.ok(roles, (long) roles.size());
    }

    @Override
    public Result mulDeleteRole(List<Long> ids) {
        List<Role> roles =roleMapper.selectBatchIds(ids);
        if(roles.size()!=ids.size()){return Result.fail("用户id不正确");}
        roleMapper.mulDelete(ids);

        return Result.ok("操作成功");
    }

    @Override
    public Result mulActiveRole(List<Long> ids) {
        List<Role> roles =roleMapper.selectBatchIds(ids);
        if(roles.size()!=ids.size()){return Result.fail("用户id不正确");}
        roleMapper.mulActive(ids);
        return Result.ok("操作成功");
    }

    @Override
    public Result mulFreezeRole(List<Long> ids) {
        List<Role> roles =roleMapper.selectBatchIds(ids);
        if(roles.size()!=ids.size()){return  Result.fail("用户id不正确");}
        roleMapper.mulFreeze(ids);
        return Result.ok("操作成功");
    }

}
